﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using Dt261.Model;
using Dt261.ViewModel;
using System.Collections.ObjectModel;
using Panuon.UI.Silver;
using Panuon.UI.Silver.Core;
using System.Threading;
using System.Windows.Threading;
using Dt261.jsonmodel;
using LiveCharts.Wpf;
using LiveCharts.Helpers;

namespace Dt261.Views
{

 

/// <summary>
/// UserPageEs.xaml 的交互逻辑
/// </summary>
public partial class ResultPageEs : Page
    {
       


        public ResultPageEs()
        {
            InitializeComponent();


            ResultPageEsVM model = new ResultPageEsVM();
            this.DataContext = model;


            model.SelectResultEvent += selectResultchanged;


            //List<TestClass> Test;
            //string NAME = "N";

            //{


            //    Test = new List<TestClass>();

            //    for (var i = 0; i < 5; i++)
            //    {
            //        Test.Add(new TestClass() { Age = i, ID = 1, Name = NAME + i.ToString() });
            //    }
            //    DG.ItemsSource = Test;

            //    //DG.AutoSizeRowsMode  = DataGridViewAutoSizeColumnsMode.AllCells;

            //    //
            //}

        }

        private void selectResultchanged()
        {
            Left.Series.Clear();
            Right.Series.Clear();

            ResultPageEsVM model = DataContext as ResultPageEsVM;
            Left.Series = model.SeriesCollection;
           // Left.AxisX = new NoisyCollection( model.Labels);
            Right.Series = model.SeriesCollection2;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;

            ResultPageEsVM model = DataContext as ResultPageEsVM;

            model.Yijian += b.Content;
            // b.Content



        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            new TestWindow().ShowDialog();
        }

        private void UpdateSelectMemberItem()
        {
            //Console.WriteLine("UpdateSelectMemberItem  start");
            //ThreadPool.QueueUserWorkItem(delegate
            //{
            //    SynchronizationContext.SetSynchronizationContext(new
            //        DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
            //    SynchronizationContext.Current.Post(pl =>
            //    {
            //        this.chartCanvas.Children.Clear();
            //        this.chartCanvas2.Children.Clear();
            //        DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
            //        DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));

            //        UserPageViewModel model = (UserPageViewModel)this.DataContext;
            //        //Task.Run(() =>
            //        model.SelectResult();
            //        //);
            //        Console.WriteLine("UpdateSelectMemberItem  end");
            //    }, null);
            //});

        }


        private void UpdateDataSets(List<ResultDataSet> DataSets, int bcneedlink = 0)
        {
            //ThreadPool.QueueUserWorkItem(delegate
            //{
            //    SynchronizationContext.SetSynchronizationContext(new
            //        DispatcherSynchronizationContext(System.Windows.Application.Current.Dispatcher));
            //    SynchronizationContext.Current.Post(pl =>
            //    {

            //        this.chartCanvas2.Children.Clear();
            //        this.chartCanvas.Children.Clear();
            //        {
            //            // this.chartCanvas.Children.Clear();
            //            DrawScale(this.chartCanvas, Color.FromRgb(0xd9, 0x4f, 0x54));
            //        }

            //        {
            //            //  this.chartCanvas2.Children.Clear();

            //            DrawScale(this.chartCanvas2, Color.FromRgb(0x55, 0xa4, 0xdd));
            //            //foreach (Shape dataEllipse in pointEllipses2)
            //            //{
            //            //    this.chartCanvas2.Children.Remove(dataEllipse);
            //            //}
            //        }
            //        foreach (ResultDataSet dataset in DataSets)
            //        {
            //            Dictionary<int, TestItem> dict = new Dictionary<int, TestItem>();
            //            foreach (ResultEntry entry in dataset.Entrys)
            //            {
            //                TestItem item = new TestItem();
            //                item.Hz = entry.ValueX;
            //                item.Db = entry.ValueY;
            //                if (entry.PointState == 1)
            //                {
            //                    item.Mask = true;
            //                    item.MaskV = 0;
            //                    //...
            //                }
            //                else
            //                {
            //                    item.Mask = false;
            //                    item.MaskV = 0;
            //                    //...
            //                }

            //                if (entry.IsUserResp == 0)
            //                    item.IsUserResp = false;
            //                else
            //                    item.IsUserResp = true;
            //                item.abc = entry.Mmtype;
            //                item.MarkType = entry.MarkType;
            //                dict.Add(item.Hz, item);

            //                //List<TestItem> listChild = this.rightResult.ToList();
            //                //listChild.Sort(delegate (TestItem p1, TestItem p2) { return Comparer<int>.Default.Compare(p1.Hz, p2.Hz); });
            //                ////List列表转为ObservableCollection集合
            //                //this.rightResult = new ObservableCollection<TestItem>(listChild);
            //            }
            //            //this.IsRightEar 
            //            UpdateTestItem(dict, dataset.IsRight == 1, dataset.HeadsetMode, bcneedlink);
            //        }

            //        Console.WriteLine("chartCanvas2.Children.Count = " + chartCanvas2.Children.Count);

            //        Console.WriteLine("chartCanvas.Children.Count = " + chartCanvas.Children.Count);
            //        //DispatcherFrame frame = new DispatcherFrame();
            //        //Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(ExitFrames), frame);
            //        //try { Dispatcher.PushFrame(frame); }
            //        //catch (InvalidOperationException) { }

            //    }, null);
            //});




        }


        private static object ExitFrames(object frame)
        {
            ((DispatcherFrame)frame).Continue = false;
            return null;

        }
       

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //var button = sender as Button;

            //var c = button.DataContext as PureToneResult;

            //var model = DataContext as UserPageViewModel;
            //new TestWindow(model.SelectMember).ShowDialog();
            // MessageBox.Show(c.Uid);

        }
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            // new TestWindow().ShowDialog();
            //var button = sender as Button;

            //// var c = button.DataContext as PureToneResult;

            //var model = DataContext as UserPageViewModel;

            //model.EditMember();
            //MessageBox.Show(c.Uid);
            // DMSkin.Core.Broadcast.PushBroadcast("Navigation", new PreviewPage(c, model.SelectMember));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //var button = sender as Button;

            //var model = DataContext as UserPageViewModel;


            //model.DeleteMember();

            //var c = button.DataContext as PureToneResult;

            //// MessageBox.Show(c.Uid);

            //var result = MessageBoxX.Show("确认删除这条测试记录？", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
            //{
            //    MessageBoxStyle = MessageBoxStyle.Modern,
            //    MessageBoxIcon = MessageBoxIcon.Warning,
            //    ButtonBrush = "#F1C825".ToColor().ToBrush(),
            //});

            //if (result == MessageBoxResult.Yes)
            //{
            //    var model = DataContext as UserPageViewModel;
            //    model.deleteTestResultByUid(c.Uid);
            //    // DialogService.ShowMessage("Delete friend successfully");
            //}


        }


        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            //try
            //{
            //    //言语
            //    var model = DataContext as UserPageViewModel;
            //    new SpeechWindow(model.SelectMember).ShowDialog();
            //}
            //catch
            //{
            //    var result = MessageBoxX.Show("言语测试遇到异常错误，请重试", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
            //    {
            //        MessageBoxStyle = MessageBoxStyle.Modern,
            //        MessageBoxIcon = MessageBoxIcon.Warning,
            //        ButtonBrush = "#F1C825".ToColor().ToBrush(),
            //    });

            //    if (result == MessageBoxResult.Yes)
            //    {

            //    }
            //}


        }


        private void Button_Click_11(object sender, RoutedEventArgs e)
        {
            //var button = sender as Button;

            ////var c = button.DataContext as PureToneResult;

            //var model = DataContext as UserPageViewModel;
            //new SpeechWindow(model.SelectMember).ShowDialog();
            ////// MessageBox.Show(c.Uid);

        }
        private void Button_Click_22(object sender, RoutedEventArgs e)
        {
            // new TestWindow().ShowDialog();
            //var button = sender as Button;

            //var c = button.DataContext as SpeechResultItem;

            //var model = DataContext as UserPageViewModel;
            //////MessageBox.Show(c.Uid);
            //DMSkin.Core.Broadcast.PushBroadcast("Navigation", new PreviewSpeechPage(c, model.SelectMember));
        }

        private void Button_Click_33(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            var c = button.DataContext as SpeechResultItem;

            // MessageBox.Show(c.Uid);

            var result = MessageBoxX.Show("确认删除这条测试记录？", "提醒", Application.Current.MainWindow, MessageBoxButton.YesNo, new MessageBoxXConfigurations()
            {
                MessageBoxStyle = MessageBoxStyle.Modern,
                MessageBoxIcon = MessageBoxIcon.Warning,
                ButtonBrush = "#F1C825".ToColor().ToBrush(),
            });

            if (result == MessageBoxResult.Yes)
            {
                //var model = DataContext as UserPageViewModel;
                //model.deleteSpeechTestResult(c);
                // DialogService.ShowMessage("Delete friend successfully");
            }
        }

        private void instScroll_Loaded(object sender, RoutedEventArgs e)
        {
            ResultsListView.AddHandler(MouseWheelEvent, new RoutedEventHandler(MyMouseWheelH), true);
        }

        private void MyMouseWheelH(object sender, RoutedEventArgs e)
        {

            MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;

            double x = (double)eargs.Delta;

            double y = instScroll.VerticalOffset;

            instScroll.ScrollToVerticalOffset(y - x);
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            DMSkin.Core.Broadcast.PushBroadcast("NavigationBack", "Update");
           // new WindowOSDA().Show();
        }
    }
}
